﻿using UnityEngine;
using System.Collections;

public class UIDragSprite : MonoBehaviour {

	public Transform dragTarget;
	public Transform dragMoveTarget;
	public bool lockYAxis = false;
	public bool reverseXAxis = false;
	public Rect dragTargetMove;
	
	bool isDragTargetMove = false;
	static int canDragBoder = 0;
	bool isDragging = false;
	
	void Start()
	{
		if(dragTarget == null)
			dragTarget = transform;
		if(dragMoveTarget == null)
			dragMoveTarget = dragTarget;
		
		dragTargetMove = ViewRect.GetInstance().BorderRect();
		
		UpdateDragPanel();
	}
	
	public void UpdateDragPanel()
	{	
		UIPanel mPanel = NGUITools.FindInParents<UIPanel>(dragTarget.gameObject);
		if (mPanel != null) 
		{			
			mPanel.clipping = UIDrawCall.Clipping.None;
		}
	}

	/// <summary>
	/// Drag the object
	/// </summary>
	void OnDrag (Vector2 delta)
	{
		isDragging = true;
		if(dragMoveTarget != null)
		{
			if(reverseXAxis)
				delta.x = -delta.x;
			if(!lockYAxis)
				dragMoveTarget.localPosition += new Vector3(delta.x, delta.y , 0);
			else
				dragMoveTarget.localPosition += new Vector3(delta.x, 0, 0);
			float revertX = 0;
			float revertY = 0;
			
			if(DragMoveTargetRect.x + canDragBoder >= (DragMoveTargetRect.width - Screen.width) * 0.5f)
			{
				revertX = (DragMoveTargetRect.width - Screen.width) * 0.5f - DragMoveTargetRect.x - canDragBoder;
				isDragTargetMove = true;
			}
			else if(DragMoveTargetRect.x - canDragBoder <= -(DragMoveTargetRect.width - Screen.width) * 0.5f)
			{
				revertX = -(DragMoveTargetRect.width - Screen.width) * 0.5f - DragMoveTargetRect.x + canDragBoder;
				isDragTargetMove = true;
			}
			
			if(!lockYAxis)
			{
				if(DragMoveTargetRect.y + DragMoveTargetRect.height < canDragBoder)
				{
					revertY = canDragBoder - (DragMoveTargetRect.y + DragMoveTargetRect.height);
				}
				else if(DragMoveTargetRect.y > Screen.height - canDragBoder)
				{
					revertY = Screen.height - canDragBoder - DragMoveTargetRect.y;
				}
			}
			
			if(!lockYAxis)
				dragMoveTarget.localPosition += new Vector3(revertX, -revertY, 0);
			else
				dragMoveTarget.localPosition += new Vector3(revertX, 0, 0);
		}
		if(dragTarget != null)
		{
			dragTarget.localPosition += new Vector3(isDragTargetMove ? -delta.x : 0, delta.y , 0);
			
			float revertX = 0;
			float revertY = 0;
			
			if(isDragTargetMove)
			{
				if(DragTargetRect.x < dragTargetMove.x)
				{
					revertX = dragTargetMove.x - DragTargetRect.x;
				}
				else if(DragTargetRect.x + DragTargetRect.width > dragTargetMove.x + dragTargetMove.width)
				{
					revertX = dragTargetMove.x + dragTargetMove.width - DragTargetRect.x - DragTargetRect.width;
				}
			}
			
			if(DragTargetRect.y > dragTargetMove.y)
			{
				revertY = dragTargetMove.y - DragTargetRect.y;
			}
			else if(DragTargetRect.y - DragTargetRect.height < dragTargetMove.y - dragTargetMove.height)
			{
				revertY = dragTargetMove.y - dragTargetMove.height - DragTargetRect.y + DragTargetRect.height;
			}
			dragTarget.localPosition += new Vector3(revertX, revertY, 0);	
		}
		
		isDragTargetMove = false;
	}

	void OnPress (bool isPressed)
	{
		if(!isPressed)
		{
			if(isDragging)
				UIManager.MakeUINodePerfectPixelPosition(dragTarget, false);
			
			isDragging = false;
		}
	}
	
	//当前对象Rect
	public Rect DragTargetRect
	{
		get
		{
			Vector3 size = UIManager.GetControlSize(dragTarget.gameObject);
//			Vector3 center = UIManager.GetControlCenterScreenPosition(dragTarget.gameObject);
//			return new Rect(center.x - size.x / 2, center.y - size.y / 2, size.x, size.y);
//			return new Rect(dragTarget.localPosition.x - size.x * 0.5f, dragTarget.localPosition.y + size.y * 0.5f,
//				size.x, size.y);
			
			//做一下 hero的大小偏移
			if(Thero.GetInstance().gameObject.Equals(dragTarget.gameObject))
				size = Vector3.Scale(size, ViewRect.GetInstance().HeroScaleV);
			
			return new Rect(dragTarget.localPosition.x, dragTarget.localPosition.y,
				size.x, size.y);
		}
	}
	
	public Rect DragMoveTargetRect
	{
		get
		{
			Vector3 size = UIManager.GetControlSize(dragMoveTarget.gameObject);
//			Vector3 center = UIManager.GetControlCenterScreenPosition(dragMoveTarget.gameObject);
//			return new Rect(center.x - size.x / 2, center.y - size.y / 2, size.x, size.y);
			return new Rect(dragMoveTarget.localPosition.x, dragMoveTarget.localPosition.y,
				size.x, size.y);
		}
	}
}
